﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dunnice.BlogIndex.Entity;
using Dunnice.BlogIndex.DLL;

namespace Dunnice.BlogIndex.BLL.LocalSiteStorage
{
    /// <summary>
    /// 本地站点生成
    /// </summary>
    public class LocalSiteMaker
    {
        public static void CreateAuthorSite(BlogAuthorInfo author)
        {
            List<BlogArticleInfo> lstArt = BlogBizControl.GetArticleContents(author);

            List<LocalPageInfo> lstPage = new List<LocalPageInfo>();

            string dir = LocalSitePathConfig.GetLocalSiteCotegoryPath();

            foreach (BlogArticleInfo art in lstArt)
            {
                string pageContent = PageMaker.MakeArticlePage(art);

                string pageName = string.Format("bb_{0}.html", art.Bid);

                LocalPageInfo page = new LocalPageInfo()
                {
                    PagePath = string.Format("{0}/{3}/{1}/{2}", dir, author.AuthorName, pageName, author.ServerName),
                    ArticleName = art.Title,
                    PageName = pageName,
                    PageBlogUrl = art.Url,
                    PagePublishTime = art.PostTime
                };

                lstPage.Add(page);

                PageStorage.SavePage(page.PagePath, pageContent);
            }

            string indexContent = PageMaker.MakeArticleIndexPage(author, lstPage);

            PageStorage.SavePage(string.Format("{0}/{2}/{1}/index.html", dir, author.AuthorName, author.ServerName), indexContent);
        }

        public static void CreateAuthorIndex(string serverName)
        {
            List<BlogAuthorInfo> authors = BlogBizControl.GetBlogAuthorInfos();

            List<LocalSiteInfo> lstSite = new List<LocalSiteInfo>();

            string dir = LocalSitePathConfig.GetLocalSiteCotegoryPath();

            foreach (BlogAuthorInfo author in authors)
            {
                lstSite.Add(new LocalSiteInfo()
                {
                    AuthorName = author.AuthorName,
                    AuthroBolgHostUrl = author.BlogUrl,
                    SitePath = dir + author.AuthorName
                });
            }

            string indexContent = PageMaker.MakeCategoryAuthorIndexPage(lstSite);

            PageStorage.SavePage(string.Format("{0}/{1}/index.html", dir, serverName), indexContent);
        }
    }

    public class LocalSiteInfo
    {
        public string SitePath;
        public string AuthorName;
        public string AuthroBolgHostUrl;
    }

    public class LocalPageInfo
    {
        public string PagePath;
        public string PageBlogUrl;
        public string PageName;
        public string ArticleName;
        public DateTime PagePublishTime;
    }
}
